package com.wgw.first.loginRegister;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.wgw.first.R;
import com.wgw.first.utils.Constants;
import com.wgw.first.utils.StreamUtils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class RegisterActivity extends AppCompatActivity {

    private static final int GET_SERVER_RESOPENSE = 1;
    private static final int GET_ERROR = 2;
    private EditText register_username;
    private EditText register_password;
    private String username;
    private String password;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case GET_SERVER_RESOPENSE:
                    System.out.println("resopense");
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();

                    break;
                case GET_ERROR:
                    System.out.println("error");
                    System.out.println(msg.obj.toString());
                    Toast.makeText(RegisterActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    break;
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_username = (EditText) findViewById(R.id.register_username);
        register_password = (EditText) findViewById(R.id.register_password);



    }

    /**
     * 注册界面的注册按钮，点击即进行注册
     *
     * @param v
     */
    public void rl2_register(View v){

        username = register_username.getText().toString().trim();
        password = register_password.getText().toString().trim();

        System.out.println("点击注册");

        new Thread(){
            @Override
            public void run() {
                System.out.println("进入注册子线程");
                //String path = "http://172.25.154.22:8080/Product/RegisterServlet?username="+ username +"&password="+ password;
                String path = Constants.BASE +Constants.REGISTER +"?username="+ username +"&password="+ password;

                try {
                    System.out.println(path);
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    int code = conn.getResponseCode();
                    if(code == 200){
                        InputStream is = conn.getInputStream();
                        String result = StreamUtils.readStream(is,1);
                        if(result.contains("1")){
                            System.out.println("注册：包含1");
                            Message msg = Message.obtain();
                            msg.what = GET_SERVER_RESOPENSE;
                            msg.obj = result;
                            handler.sendMessage(msg);

                        }else{
                            System.out.println("注册：不包含1");
                            Message msg = Message.obtain();
                            msg.what = GET_ERROR;
                            msg.obj = result;
                            System.out.println(result);
                            handler.sendMessage(msg);
                        }

                    }else{
                        Message msg = Message.obtain();
                        msg.what = GET_ERROR;
                        msg.obj = "服务器正在维修";
                        handler.sendMessage(msg);
                    }



                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = GET_ERROR;
                    handler.sendMessage(msg);
                }


            }
        }.start();

        

    }

    /**
     *  注册界面的（已有账号登录）按钮，点击跳转到主界面
     */
    public void rl2_login(View v){
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

}
